package com.steel.yfmall.cloud.assistant.user.impl;

import com.steel.yfmall.cloud.assistant.dto.para.user.ParaUserCompanyBean;
import com.steel.yfmall.cloud.assistant.po.user.UserCompany;
import com.steel.yfmall.cloud.assistant.po.user.UserCompanyExample;
import com.steel.yfmall.cloud.assistant.user.UserCompanyService;
import com.steel.yfmall.cloud.assistant.user.mapper.UserCompanyMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: UserCompanyServiceImpl
 * @Package com.steel.yfmall.cloud.assistant.user.impl
 * @Description: (用一句话描述该文件做什么)
 * @date 2021/2/19 17:41
 */
@Slf4j
@Service
public class UserCompanyServiceImpl implements UserCompanyService {

    /**
     * 获取企业信息
     * @Title: getUserCompanyCodeMap
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.Map<java.lang.String,com.steel.yfmall.cloud.assistant.po.user.UserCompany>    返回类型
     * @author yuchaohua
     * @date 2021/2/19 17:52
     */
    @Override
    public Map<String, UserCompany> getUserCompanyCodeMap(ParaUserCompanyBean paraBean){
        Map<String, UserCompany> returnMapBean = null;
        UserCompanyExample example = new UserCompanyExample();
        UserCompanyExample.Criteria criteria = example.createCriteria();
        getSearchData(criteria,paraBean);
        List<UserCompany> userCompanyList = userCompanyMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(userCompanyList)){
            returnMapBean = new HashMap<>();
            for(UserCompany item : userCompanyList){
                returnMapBean.put(item.getCompanyCode(),item);
            }
        }
        return returnMapBean;
    }

    /**
     * 获取企业信息集合
     * @Title: getUserCompanyListByPara
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.List<com.steel.yfmall.cloud.assistant.po.user.UserCompany>    返回类型
     * @author yuchaohua
     * @date 2021/2/20 10:30
     */
    @Override
    public List<UserCompany> getUserCompanyListByPara(ParaUserCompanyBean paraBean){
        UserCompanyExample example = new UserCompanyExample();
        UserCompanyExample.Criteria criteria = example.createCriteria();
        getSearchData(criteria,paraBean);
        if(CollectionUtils.isEmpty(example.getOredCriteria())){
            return null;
        }
        List<UserCompany> userCompanyList = userCompanyMapper.selectByExample(example);
        return userCompanyList;
    }

    @Override
    public UserCompany getUserCompanyByPara(ParaUserCompanyBean paraBean) {
        UserCompanyExample example = new UserCompanyExample();
        UserCompanyExample.Criteria criteria = example.createCriteria();
        getSearchData(criteria,paraBean);
        if(CollectionUtils.isEmpty(example.getOredCriteria())){
            return null;
        }
        return userCompanyMapper.selectOneByExampleSelective(example);
    }

    private void getSearchData(UserCompanyExample.Criteria criteria, ParaUserCompanyBean paraBean) {
        if (criteria == null || paraBean == null){
            return;
        }
        if (CollectionUtils.isNotEmpty(paraBean.getCompanyCodeList())){
            criteria.andCompanyCodeIn(paraBean.getCompanyCodeList());
        }
        if (StringUtils.isNotBlank(paraBean.getCompanyCode())){
            criteria.andCompanyCodeEqualTo(paraBean.getCompanyCode());
        }
        if (StringUtils.isNotBlank(paraBean.getCompanyNameLike())) {
            criteria.andCompanyNameLike("%" + paraBean.getCompanyNameLike() + "%");
        }
    }


    @Resource
    private UserCompanyMapper userCompanyMapper;

}
